package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 向购物车添加商品
    * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShoppingCartDTO shoppingCartDTO){
        //获取用户id，菜品id和套餐id
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        //查询该商品是否已经被添加过
        ShoppingCart shoppingCartNow = shoppingCartMapper.get(userId,dishId,setmealId);
        if(shoppingCartNow != null){
            //如果已经添加过，则数量加1
            shoppingCartMapper.changeNumber(shoppingCartNow.getId(),1);
            return;
        }
        //未被添加过
        if(dishId != null){
            //添加菜品
            Dish dish = dishMapper.selectById(dishId);
            ShoppingCart shoppingCart = ShoppingCart.builder()
                    .userId(userId)
                    .dishId(dishId)
                    .image(dish.getImage())
                    .amount(dish.getPrice())
                    .name(dish.getName())
                    .dishFlavor(shoppingCartDTO.getDishFlavor())
                    .number(1)
                    .createTime(LocalDateTime.now())
                    .build();
            //加入数据库shopping_cart表
            shoppingCartMapper.add(shoppingCart);
        }else{
            //添加套餐
            Setmeal setmeal = setmealMapper.selectById(setmealId);
            ShoppingCart shoppingCart = ShoppingCart.builder()
                    .userId(userId)
                    .name(setmeal.getName())
                    .amount(setmeal.getPrice())
                    .image(setmeal.getImage())
                    .number(1)
                    .setmealId(setmealId)
                    .createTime(LocalDateTime.now())
                    .build();
            //加入数据库shopping_cart表
            shoppingCartMapper.add(shoppingCart);
        }
    }

    /*
    *删除购物车的一个商品
    * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sub(ShoppingCartDTO shoppingCartDTO){
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        //获取要删除的商品在购物车表的number，如果为1，则删除该行，如果不为1，则number减1
        ShoppingCart shoppingCartNow = shoppingCartMapper.get(userId,dishId,setmealId);
        if(shoppingCartNow == null){
            throw new BaseException(MessageConstant.GOOD_IN_SHOPPING_CART_NOT_EXIST);
        }

        if(shoppingCartNow.getNumber() == 1){
            //直接删除该行
            shoppingCartMapper.deleteById(shoppingCartNow.getId());
        }else{
            //将该商品数量减一
            shoppingCartMapper.changeNumber(shoppingCartNow.getId(),-1);
        }

    }

    /*
    * 查看购物车
    * */
    @Override
    public List<ShoppingCart> list(){
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectByUserId(userId);
    }

    /*
    * 清空购物车
    * */
    @Override
    public void clean(){
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
